---
title: "analyze_prereg"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r}
rm(list = ls())
library(data.table); library(tidyverse); library(marginaleffects); library(afex); library(hausekeep); library(effectsize); library(ggbeeswarm)
theme_set(theme_minimal())
# source("utils.R")
```

```{r}
d0 <- fread("../data/clean/data-long.csv")
glimpse(d0)
d0 <- d0[veracity != "none"]
# d0 <- d0[screenacc == 1.0]
unique(d0$responseid)
summary(d0)

```

```{r recode}
d0[, table(condition)]
d0[, condition := factor(condition)]
d0[, condition := relevel(condition, "debunk")]

d0[, table(veracity)]
d0[, veracityc := ifelse(veracity == "false", -0.5, 0.5)]
d0[, table(veracityc)]

d0[, responseid := factor(responseid)]
glimpse(d0)

d0[, condition := factor(condition, levels = c("misinfo-only", "correction-only", "debunk"), 
                         labels = c("Misinfo", "Correction", "Debunk"))]
d0
```


```{r prereg models}
d1 <- d0[condition %in% c("Debunk", "Misinfo")]
d1[, table(condition)]
d1
m1 <- aov_ez("responseid", "videorating", d1, between = "condition", within = "veracity")
summary(m1)
cohens_f(m1)  # cohen's f is same as cohen's d when factors have 2 levels
mfx1 <- marginaleffects(m1, by = c("veracity"))
tidy(mfx1) |> mutate_if(is.numeric, round, 3)

d2 <- d0[condition %in% c("Correction", "Misinfo")]
d2[, table(condition)]
m2 <- aov_ez("responseid", "videorating", d2, between = "condition", within = "veracity")
summary(m2)
cohens_f(m2)
mfx2 <- marginaleffects(m2, by = c("veracity"))
tidy(mfx2) |> mutate_if(is.numeric, round, 3)

d3 <- d0[condition %in% c("Correction", "Debunk")]
d3[, table(condition)]
m3 <- aov_ez("responseid", "videorating", d3, between = "condition", within = "veracity")
summary(m3)
cohens_f(m3)
mfx3 <- marginaleffects(m3, by = c("veracity"))
tidy(mfx3) |> mutate_if(is.numeric, round, 3)
```


```{r}
d0[, veracity := ifelse(veracityc == 0.5, "True", "False")]
mean_ci <- seWithin(d0, "videorating", "condition", "veracity", "responseid")

mapper <- c(Misinfo = "Misinformation-only", Correction = "Correction-only", Debunk = "Correction after misinformation (debunking)")
mean_ci[, condition := recode_factor(condition, !!!mapper)]
d0[, condition := recode_factor(condition, !!!mapper)]

ggplot(mean_ci, aes(condition, videorating, col = veracity)) +
    geom_quasirandom(data = d0, alpha = 0.2, dodge = 0.8, size = 0.6) +
    geom_point(position = position_dodge(0.8), shape = 95, size = 6) +
    geom_errorbar(aes(ymin = videorating - ci, ymax = videorating + ci), position = position_dodge(0.8), width = 0, size = 1.1) +
    scale_colour_viridis_d(begin = 0.5, end = 0.0, option = 'magma') +
    labs(x = "Condition", y = "TikTok video credibility", col = "Veracity")
ggsave("../figures/means.png", dpi = 300, bg = 'white', width = 8, height = 5)
```

